#include <views/taiyunview.h>
#include <ui_taiyunview.h>
#include <QColor>

TaiyunView::TaiyunView(QWidget *parent) :
	AbstractView(parent),
    ui(new Ui::TaiyunView)
{
    ui->setupUi(this);
    network = NULL;
	heatmap = new HeatMap(this);
	heatmap->setMinimumHeight(150);
	heatmap->setMinimumWidth(150);
	ui->horizontalLayout->addWidget(heatmap);

	connect(heatmap,SIGNAL(elementSelected(int, int)),this,SLOT(on_elementSelected(int,int)));
	connect(heatmap,SIGNAL(elementClicked(int, int)),this,SLOT(on_elementClicked(int,int)));
	connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(on_currentIndexChanged(int)));
}

TaiyunView::~TaiyunView()
{
    delete ui;
}

void TaiyunView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void TaiyunView::resizeEvent(QResizeEvent *ev)
{
	updateView();
}

void TaiyunView::on_matrixChanged()
{
	AbstractView::on_matrixChanged(); //will call updateView
	setHeatmapNetwork();
	heatmap->update();
}

void TaiyunView::setHeatmapNetwork(){
	// set matrix based on the current slider value
	// TODO: is this really needed? 
	if(ui->comboBox->currentIndex () == 0)
		heatmap->setMatrix(network->getCurrentIndividualMatrix());
	else if(ui->comboBox->currentIndex() == 1){ // current-vs-average
		CorrelationMatrix *corrm = new CorrelationMatrix();
		for(unsigned int i =0; i < network->getCurrentIndividualMatrix()->height(); i++){
			std::vector<double> row;
			for(unsigned int j = 0; j < network->getCurrentIndividualMatrix()->width(); j++){
				row.push_back((network->getCurrentIndividualMatrix()->at(i,j)-network->getCurrentAverageMatrix()->at(i,j)));
			}
			corrm->addRow(row);
		}
		heatmap->setMatrix(corrm);
	}
	else if(ui->comboBox->currentIndex() == 2){ // current-vs-average
		CorrelationMatrix *corrm = new CorrelationMatrix();
		for(unsigned int i =0; i < network->getCurrentIndividualMatrix()->height(); i++){
			std::vector<double> row;
			for(unsigned int j = 0; j < network->getCurrentIndividualMatrix()->width(); j++){
				if(network->getLink(i,j) != NULL)
					row.push_back((network->getCurrentIndividualMatrix()->at(i,j)-(1/pow(network->getLink(i,j)->distance(),2))));
				else{ row.push_back(0); }
			}
			corrm->addRow(row);
		}
		heatmap->setMatrix(corrm);
	}
}

void TaiyunView::on_elementSelected(int row, int col) //mouse hover
{
		// Update the Constellation matrix
		//----------------------------------------
		//int lX, lY, rX, rY;
		//lX=std::max(col-1,0);
		//rX=std::min(col+1,(int)network->getCurrentIndividualMatrix()->width()-1);

		//lY=std::max(row-1,0);
		//rY=std::min(row+1,(int)network->getCurrentIndividualMatrix()->height()-1);
		if(row != 0 && col != network->getCurrentIndividualMatrix()->width()){
			QString text("");
			QPoint *p = heatmap->getLookupMatrix().at(row-1).at(col);

			AnatomicalLink * link = network->getLink(p->x(),p->y());
			
			if(!link) //link doesn't exist
				return;

			double corr = link->correlation();
			double dist = link->distance();
			double dev = corr - (1.0/(dist*dist));
			text+=tr("<b>Link between %1 and %2</b>").arg(link->regionA()->info().ShortName.c_str()).arg(link->regionB()->info().ShortName.c_str());
			text+=tr("<hr>");
			text+=tr("<table>");
			text+=tr("<tr><td><b>Correlation</b></td><td>%1</td></tr>").arg(corr);
			text+=tr("<tr><td><b>Distance</b></td><td>%1</td></tr>").arg(dist);
			text+=tr("<tr><td><b>Devation from 1/D&#178;</b></td><td>%1</td></tr>").arg(dev);
			text+=tr("</table>");
			text+=tr("row: %1 - col: %2").arg(row).arg(col);
			//text+=tr("<b>Zoomed region<br>");


			QToolTip::showText(QCursor::pos(),text,this);
		}


}

void TaiyunView::updateView()
{
	heatmap->update();
	if(network)
		heatmap->recreate();
}

void TaiyunView::on_elementClicked(int row, int col)
{
	/*if(row != 0 && col != heatmap->im->width()){*/
		SelectedLinks links;
		QPoint *p = heatmap->getLookupMatrix().at(row-1).at(col); //first row is to show selection so you click row+1 in lookup = row.
		
		links.push_back(network->getLink(p->x(), p->y()));
		setSelection(links);
	//}
}

void TaiyunView::on_currentIndexChanged(int idx){
	setHeatmapNetwork();
	heatmap->update();
}

void TaiyunView::setNetwork(FunctionalNetwork *netw)
{
	AbstractView::setNetwork(netw);
	heatmap->setNetwork(netw);
	heatmap->setMatrix(network->getCurrentIndividualMatrix());
	heatmap->createLookupMatrix();
	updateView();
}

void TaiyunView::createPixMap()
{
	CorrelationMatrix *currentIndividualMatrix = network->getCurrentIndividualMatrix();
	CorrelationMatrix *currentAverageMatrix = network->getCurrentAverageMatrix();
}